<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
$strGameDate = (empty($_data["GameDate"]))?DateForm(date("Y-m-d"), date("H:i:s"), "Y-m-d"):$_data["GameDate"];
$intLottoID = (int)$_data["LottoID"];
$a = explode("-", $strGameDate);
$strGameDate = date("Y-m-d",mktime(0, 0, 0, $a[1], $a[2], $a[0]));
$strNextDate = date("Y-m-d",mktime(0, 0, 0, $a[1], $a[2] + 1, $a[0]));
$strPreDate = date("Y-m-d",mktime(0, 0, 0, $a[1], $a[2] - 1, $a[0]));
$strOtherDate = date("Ymd",mktime(0, 0, 0, $a[1], $a[2], $a[0]));

$strSQL = "select a.ID as ID, a.LottoID as LottoID, a.GameDate as GameDate, a.GameTime as GameTime, a.EndGameTime as EndGameTime, a.SerialNo as SerialNo, a.OSerialNo as OSerialNo, a.isEnd as isEnd, a.PayupDate as PayupDate, b.Lotto_".$_intLang." as Lotto from `bet_Gamble` a, `bet_Lotto` b where a.PayupDate = '".$strGameDate."' and a.LottoID = b.ID ".($intLottoID > 0?" and a.LottoID = ".$intLottoID:"")." order by a.LottoID, a.SerialNo desc";
//print $strSQL;
if(!$RS = mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errormessage"] = "Find Gamble Data Error!!";
	$GLOBALS["errorhidden"] = "\n Error: ".mysql_error($$_Conn).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}
$intTotal = PMA_mysql_num_rows($RS);

$fp = @file($strTemplatePath."List.html");
$fp = implode("", $fp);
$fp = setHTMLCharset($fp);
$fp = str_replace("{ShowGameDate}", $strGameDate, $fp);
$fp = str_replace("{ShowLottoID}", $intLottoID, $fp);
$fp = str_replace("{ShowPreDate}", $strPreDate, $fp);
$fp = str_replace("{ShowNextDate}", $strNextDate, $fp);

$intNoQAdd = 0;
$arrQAdd = array(5, 6, 7, 8);
$arrLottoPower = getLottoUsePowerArray();
foreach($arrQAdd as $intLID){
	if(!in_array($intLID, $arrLottoPower)){
		$fp = preg_replace("/<!-- @ QAdd".$intLID." Start @ \/\/-->.+<!-- @ QAdd".$intLID." End @ \/\/-->/s", "", $fp);
		$intNoQAdd++;
	}
	$fp = str_replace("{QAddL".$intLID."}", $_arrLotto[$intLID]["Lotto_".$_intLang], $fp);
}
if($intNoQAdd >= sizeof($arrQAdd))$fp = preg_replace("/<!-- @ QAdd Start @ \/\/-->.+<!-- @ QAdd End @ \/\/-->/s", "", $fp);

$strLOption = "<option value=\"\">--select--</option>\n";
foreach($_arrLotto as $intLID => $arrLottoData){
	if(!in_array($intLID, $arrLottoPower))continue;
	$strLOption .= "<option value='".$intLID."' ".(($intLottoID == $intLID)?"selected":"").">".$arrLottoData["Lotto_".$_intLang]."</option>\n";
}
$strLOption .= "</select>\n";
$fp = str_replace("{LottoIDOption}", $strLOption, $fp);

while(list($k, $v) = each($_strCompetition)){
	$fp = str_replace("{".$k."}", $v, $fp);
}

$arrOther = array(5 => "txt", 6 => "sh", 7 => "cq", 8 => "hlj");
$arrOhterScore = array();
foreach($arrOther as $intLID => $strL){
	$strFile = "/home/share/common/others/".$strOtherDate."/result.".$strL;
	if(file_exists($strFile)){
		$arrOhterScore[$intLID] = unserialize(implode("", file($strFile)));
	}
}

//內容清單
$strCon = "";
$strTmp = "";
preg_match("/<!-- @ Content Start @ \/\/-->(.+)<!-- @ Content End @ \/\/-->/s", $fp, $arrMatch);

$strNow = DateForm(date("Y-m-d"), date("H:i:s"), "Y-m-d H:i:s");
while($objGamble = PMA_mysql_fetch_object($RS)){
	$strTmp = $arrMatch[1];
	$strExport = "";

	$isOverTime = ($objGamble -> isEnd == 1 && $objGamble -> PayupDate < date("Y-m-d"))?true:false;
	$strGDate = DateForm($objGamble -> GameDate, $objGamble -> GameTime, "Y-m-d");
	$strGTime = TimeForm($objGamble -> GameDate, $objGamble -> GameTime, "H:i:s");
	$strEndGTime = TimeForm($objGamble -> GameDate, $objGamble -> EndGameTime, "H:i:s");
	$strNumber = "&nbsp;";

	$strConCat = "NumberNormal";
	$arrOScore = $arrOhterScore[$objGamble -> LottoID];
	$strOScore = "";
	switch($objGamble -> LottoID){
		case 5:
			$strSNo = substr($objGamble -> SerialNo, -2);
			if(is_array($arrOScore)){
				foreach($arrOScore as $intSNo => $arrT){
					if($arrT[0] < "06:00:00")continue;

					if((int)$strSNo == ($intSNo % 100)){
						$arrN = explode(",", $arrT[1]);
						$strOScore = $arrN[0].",".$arrN[1].",".$arrN[2].",".$arrN[3]." + ".$arrN[4];
						break;
					}
				}
			}
		case 1:
			$strConCat = "CONCAT(NumberNormal, ' + ', NumberSpecial)";
			break;

		case 6:
			$strSNo = sprintf("%02s", substr($objGamble -> SerialNo, -2));
			$strOScore = $arrOScore[$strSNo]["Number"];
			break;

		case 7:
			$strSNo = substr($objGamble -> SerialNo, -3);
			$strSNo = sprintf("%03s", $strSNo);
			$strOScore = $arrOScore[$strSNo]["Number"];
			break;

		case 8:
			$strSNo = $objGamble -> OSerialNo;
			$strOScore = $arrOScore[$strSNo]["Number"];
			break;
	}
	$strOScore = str_replace(",", ", ", $strOScore);
	
	if($isOverTime){
		$strEdit = "&nbsp;";
		$strDel = "&nbsp;";
		$strChScore = "&nbsp;";
		if($objGamble -> isEnd == 1){
			$strNumber = GetRelationType3 ("`bet_GambleScore`", " GambleID = ".$objGamble -> ID, " ID, ".$strConCat." as Value ");
			$strNumber = str_replace(",", ", ", $strNumber);
		}
	}else{ //if($isOverTime)
		if($objGamble -> isEnd == 1){
			$strEdit = "&nbsp;";
			$strDel = "&nbsp;";
			$strClass = "red_link";
			$strScoreTip = $_strCompetition["List_ChangeScore"];
			$strNumber = GetRelationType3 ("`bet_GambleScore`", " GambleID = ".$objGamble -> ID, " ID, ".$strConCat." as Value ");
			$strNumber = str_replace(",", ", ", $strNumber);
		}else{
			$strEdit = "<a href='JavaScript:Edit(".$objGamble -> ID.")'>".$_strCompetition["_Edit"]."</a>";
			$strDel = "<a href='JavaScript:Del(".$objGamble -> ID.",".$objGamble -> LottoID.")'>".$_strCompetition["_Del"]."</a>";
			$strClass = "";
			$strScoreTip = $_strCompetition["List_InsertScore"];
			$strOScore = "";

			if(__isAllowExport == 1 && $strNow > $objGamble -> GameDate." ".$objGamble -> EndGameTime)$strExport = "Export";
		}
		$strChScore = "<a href='JavaScript:Result(".$objGamble -> ID.")' class='".$strClass."'>".$strScoreTip."</a>";
	} //if($isOverTime)
	if($strOScore != "" && $strNumber != ""){
		if(strpos($strOScore, $strNumber) !== false){
			$strNumber .= " &nbsp;&nbsp;( ".$strOScore." )";
		}else{
			$strNumber .= " &nbsp;&nbsp;( <a style='color: #FF0000; font-weight: 700'>".$strOScore."</a> )";
		}
	}

	$strTmp = str_replace("{ShowID}", $objGamble -> ID, $strTmp);
	$strTmp = str_replace("{ShowItemLottoID}", $objGamble -> LottoID, $strTmp);
	$strTmp = str_replace("{ShowDate}", $strGDate, $strTmp);
	$strTmp = str_replace("{ShowTime}", $strGTime, $strTmp);
	$strTmp = str_replace("{ShowEndGameTime}", $strEndGTime, $strTmp);
	$strTmp = str_replace("{ShowSerialNo}", $objGamble -> SerialNo, $strTmp);
	if($objGamble -> OSerialNo){
		$strTmp = str_replace("{ShowOSerialNo}", $objGamble -> OSerialNo, $strTmp);
	}else{
		$strTmp = str_replace("{ShowOSerialNo}", "&nbsp;", $strTmp);
	}
	$strTmp = str_replace("{ShowLotto}", $objGamble -> Lotto, $strTmp);
	$strTmp = str_replace("{ShowEdit}", $strEdit, $strTmp);
	$strTmp = str_replace("{ShowDel}", $strDel, $strTmp);
	$strTmp = str_replace("{ShowChangeScore}", $strChScore, $strTmp);
	$strTmp = str_replace("{ShowNumber}", $strNumber, $strTmp);
	$strTmp = str_replace("{ShowExport}", $strExport, $strTmp);

	$strCon .= $strTmp."\n";
} //while($objLotto = mysql_fetch_object($RS)){
$fp = str_replace("{Sum_MNo}", $intTotal, $fp);
$fp = preg_replace("/<!-- @ Content Start @ \/\/-->.+<!-- @ Content End @ \/\/-->/s", $strCon, $fp);
if(__isAllowExport != 1)$fp = preg_replace("/<!-- @ ExportTip Start @ \/\/-->.+<!-- @ ExportTip End @ \/\/-->/s", "", $fp);

$DD = '"序号","下注日期","下注时间","账号","退水","下注种类","注单内容","注额","结果","方式","状态"';

//輸出
WebOutputHTML($fp, 1);
?>